package com.eapi.project.controller;

import com.eapi.eapicommom.model.entity.User;
import com.eapi.project.annotation.AuthCheck;
import com.eapi.project.common.BaseResponse;
import com.eapi.project.model.dto.article.AddCategoryRequest;
import com.eapi.project.model.vo.CategoryVo;
import com.eapi.project.service.CategoryService;
import com.eapi.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Resource
    UserService userService;
    // 前端界面显示分类列表
    @GetMapping("/getCategoryList")
    public BaseResponse<List<CategoryVo>> getCategoryList(){
       return categoryService.getCategoryList();
    }

    // 发布文章时，选择分类
    // 显示出所有分类
    @GetMapping("/getAllCategoryList")
    public BaseResponse<List<CategoryVo>> getAllCategoryList(){
        return categoryService.getAllCategoryList();
    }

    //新增category
    @AuthCheck(mustRole = "admin")
    @PostMapping("/addCategory")
    public BaseResponse<String> addCategory(@RequestBody AddCategoryRequest addCategoryRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return categoryService.addCategory(addCategoryRequest, loginUser);
    }
}
